package org.helgoboss.janana_tray_exit

import org.helgoboss.osgi_additions.{OsgiProvider, OsgiContext, OsgiConsumer, OsgiLogging}
import org.helgoboss.janana.Janana
import java.awt.MenuItem
import java.awt.event.{ActionEvent, ActionListener}
import org.helgoboss.janana_tray.{JananaTray, JananaTrayAddOn}

class JananaTrayExit extends OsgiProvider with OsgiContext with OsgiConsumer with OsgiLogging {
    def janana = requiredService[Janana]
        
    whenBundleActive {
        trayAddOn.providesService[JananaTrayAddOn]
    }
    
    val trayAddOn = new JananaTrayAddOn {
        lazy val exitItem = {
            val exitListener = new ActionListener {
                def actionPerformed(e: ActionEvent) {
                    janana.stop
                }
            }
                    
            val tmp = new MenuItem("Exit")
            tmp.addActionListener(exitListener)
            tmp
        }
        
        def addToJananaTray(tray: JananaTray) {
            tray.getIcon.getPopupMenu.add(exitItem)
        }

        def removeFromJananaTray(tray: JananaTray) {
            tray.getIcon.getPopupMenu.remove(exitItem)
        }
    }
}